#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

typedef struct 
{
    string s;
    int y;
    int m;
    int d;
} Data;

bool compare(const Data& a, const Data& b)
{
    if (a.y > b.y) return true;
    if (a.y < b.y) return false;
    if (a.m > b.m) return true;
    if (a.m < b.m) return false;
    if (a.d > b.d) return true;
    else return false;
}

int main(int argc, char const *argv[])
{
    int n, i;
    Data datas[128];
    
    cin >> n;
    for (i = 0; i < n; i++)
        cin >> datas[i].s 
            >> datas[i].y
            >> datas[i].m
            >> datas[i].d;

    stable_sort(datas, datas + n, compare);
    reverse(datas, datas + n);

    for (i = 0; i < n - 1; i++)
        cout << datas[i].s 
            << endl;
    
    cout << datas[n - 1].s;

    return 0;
}
